<?php
namespace app\admin\logic;
use app\admin\validate\User as UserValidate;

class UserLogic extends BaseLogic {

    /**
     * 获取用户查询条件
     * @param $username
     * @param $department
     * @param $position
     * @param $status
     * @return array
     */
    public function get_search_user_maps($username,$department,$position,$status){
        $maps = [];
        $maps[] = ['role','>',1];
        $maps[] = ['username','<>',''];

        if(!empty($username)){
            $maps[] = ['username','like','%' . $username . '%'];
        }
        if(!empty($department)){
            $maps[] = ['department','like','%' . $department . '%'];
        }
        if(!empty($position)){
            $maps[] = ['position','like','%' . $position . '%'];
        }
        if($status != -1){
            $maps[] = ['status','=',$status];
        }

        return $maps;
    }

    /**
     * 检查修改用户规则
     * @param $admin_role
     * @param $params
     * @return mixed
     */
    public function check_save_user_rule($admin_role,$params){
        $validate = new UserValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['userid']];
        $u_detail =  model('user')->get_detail($maps,'id,openid,role');
        if(empty($u_detail)){
            return $this->get_response(0,'该记录不存在');
        }

//        if($u_detail['role'] <= $admin_role){
//            return $this->get_response(0,'不能修改同级用户');
//        }
//
//        if($params['role'] <= $admin_role){
//            return $this->get_response(0,'不能修改为同级用户');
//        }

        $data['u_detail'] = $u_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查审核规则
     * @param $admin_role
     * @param $params
     * @return mixed
     */
    public function check_examine_rule($admin_role,$params){
        $maps = [];
        $maps[] = ['id','=',$params['userid']];
        $u_detail =  model('user')->get_detail($maps,'id,role,status');
        if(empty($u_detail)){
            return $this->get_response(0,'该记录不存在');
        }
        if($u_detail['status'] != 0){
            //return $this->get_response(0,'该记录已审核');
        }

        if($u_detail['role'] <= $admin_role){
            return $this->get_response(0,'不能操作同级用户');
        }

        $data['u_detail'] = $u_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查删除用户规则
     * @param $admin_role
     * @param $params
     * @return mixed
     */
    public function check_del_user_rule($admin_role,$params){
        $maps = [];
        $maps[] = ['id','=',$params['userid']];
        $u_detail =  model('user')->get_detail($maps,'id,role');
        if(empty($u_detail)){
            return $this->get_response(0,'该记录不存在');
        }

//        if($u_detail['role'] <= $admin_role){
//            return $this->get_response(0,'不能操作同级用户');
//        }

        $data['u_detail'] = $u_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 修改用户权限
     * @param $openid
     * @param $params
     * @return mixed
     */
    public function save_user_power($openid,$params){
        $params['admin_power'] = ($params['role'] != 4) ? $params['admin_power'] : [];

        $maps = [];
        $maps[] = ['openid','=',$openid];
        $up_detail = model('user_power')->get_detail($maps,'id');
        if(empty($up_detail)){
            $up_data = [];
            $up_data['openid'] = $openid;
            $up_data['admin_power'] = json_encode($params['admin_power'],JSON_UNESCAPED_UNICODE);
            $up_data['user_power'] = json_encode($params['user_power'],JSON_UNESCAPED_UNICODE);
            $r = model('user_power')->insert($up_data);
        }else{
            $up_data = [];
            $up_data['admin_power'] = json_encode($params['admin_power'],JSON_UNESCAPED_UNICODE);
            $up_data['user_power'] = json_encode($params['user_power'],JSON_UNESCAPED_UNICODE);

            $maps = [];
            $maps[] = ['id','=',$up_detail['id']];
            $r = model('user_power')->force()->save($up_data,$maps);
        }
        if($r !== false){
            return $this->get_response(1,'修改成功');
        }
        return $this->get_response(0,'修改失败');
    }
}